package com.atldy.redis.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import com.atldy.redis.service.HyperloglogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <h2> TODO </h2>
 *
 * @Author: li-dong-yang
 * @CreateTime: 2021-11-11 23:09
 */
@Slf4j
@Service
public class HyperloglogServiceImpl implements HyperloglogService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 启动运行初始化操作，模拟点击
     */
    @Override
    //@PostConstruct
    public void initialize() {
        ThreadUtil.execute(() -> {
            for (int i = 0; i < 200; i++) {
                log.error("初始化模拟uv操作");
                String ip = RandomUtil.randomInt(1, 255) + "." + RandomUtil.randomInt(1, 255) + "." + RandomUtil.randomInt(1, 255) + "." + RandomUtil.randomInt(1, 255);
                Long all = redisTemplate.opsForHyperLogLog().add("all", ip);

                log.error("访问IP ；[{}] ,登陆次数是：[{}] []", ip, all);
                ThreadUtil.sleep(2, TimeUnit.SECONDS);
            }
        });
    }
}